package com.lglin.dao.user;

import com.lglin.bean.product.entity.Order;
import com.lglin.bean.user.entity.Address;
import com.lglin.bean.user.entity.User;
import com.lglin.common.bean.Review;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface UserDao {
    @Insert("insert into user(email,password,nickname,img, createtime)values(#{email},#{password},#{nickname},#{img}, now())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int register(User user);

    @Select("select * from user where nickname = #{nickname}")
    User checkNickname(String nickname);

    @Select("select * from user where email = #{email}")
    User checkEmail(String email);

    @Update(" update user set nickname=#{nickname},img=#{img} WHERE id=#{id}")
    int updateUserInfo(@Param("id") int id, @Param("img") String email, @Param("nickname") String nickname);

    @Select("select * from user where id = #{id}")
    User selectById(int id);

    @Select("select id,nickname,img from user where id = #{id}")
    User selectNameAndImgById(int id);

    @Insert("insert into review(pid,uid,time,star,content)values(#{pid},#{uid},#{time},#{star},#{content})")
    int insertReview(Review review);

    @Select("select aid from user_address where uid = #{id}")
    List<Integer> selectUserAddressIdByUId(int id);

    List<Address> selectAddressesById(List<Integer> userAddressIds);

    @Update(" update address set consignee=#{consignee},phone=#{phone},province=#{province},city=#{city},area=#{area},street=#{street}WHERE id=#{id}")
    int updateAddressById(Address address);

    @Insert("insert into address(consignee,phone,province,city,area,street,flag)values(#{consignee},#{phone},#{province},#{city},#{area},#{street},0)")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertAddress(Address address);

    @Delete("delete from user_address where aid=#{id}")
    int deleteAddressById(int id);

    @Delete("delete from address where id=#{id}")
    int deleteUserAddressRelate(int id);

    @Update("update user set password=#{password} where email=#{email}")
    int updatePasswordByEmail(@Param("email") String email, @Param("password") String encode_password);

    @Insert("insert into `order`(uid,aid,osid,sid,time,message)values(#{uid},#{aid},1,#{sid},now(),#{message})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertOrder(Order order);

    int insertOrderProduct(@Param("oid") int oid, @Param("map") Map<Integer, Integer> map);

    @Select("select o.sid, o.time, sum(op.num*p.new_price) as new_price, os.status from `order` o join order_status os on os.id = o.osid join order_product op on op.oid = o.id join product p on op.pid = p.id group by op.oid")
    List<Map<String, Object>> userOrder(int uid);

    @Select("select p.id, p.name, p.new_price, op.num, op.num*p.new_price as total_price, pic.path from order_product op join product p on op.pid = p.id  join picture pic on p.id = pic.pid join `order` o on o.id = op.oid where o.sid = #{sid} group by o.id")
    List<Map<String, Object>> orderInfo(String sid);
}
